﻿using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using LightWeightORM.ORM.TableAccessor;
using SmartWarehouse.Common;
using SmartWarehouse.ORM.Entity;
using SmartWarehouse.Config;

namespace SmartWarehouse
{
    namespace BussinessLogic
    {
        namespace Basis
        {
            /// <summary>
            /// Summary description for UserFacade
            /// </summary>
            public class UserFacade
            {
                private TableAccessor<SwUser> _userAccessor;

                public TableAccessor<SwUser> UserAccessor
                {
                    get { return _userAccessor; }
                    set { _userAccessor = value; }
                }

                public UserFacade()
                {
                    this.UserAccessor = AccessorFactory.UserAccessor;
                }

                public SwMessage CheckUser(string name, string password)
                {
                    SwMessage message = new SwMessage(SwMessageType.Ok, "验证成功");

                    //用户是否存在
                    if (this.UserAccessor.RecordCount(new Filter(SwUser._Name, name, ComparisonOperator.Equal)) == 0)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = "该用户不存在";
                        return message;
                    }
                    //密码是否正确
                    SwUser user = UserAccessor.GetOne(new Filter(SwUser._Name, name, ComparisonOperator.Equal));
                    if (user.Password != password)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = "密码错误";
                        return message;
                    }
                    //用户是否被锁定
                    if (user.Status == UserStatus.Locked)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = "用户已被锁定";
                        return message;
                    }

                    return message;
                }

                public SwMessage AddUser(SwUser user)
                {
                    SwMessage message = new SwMessage(SwMessageType.Ok, "用户已成功添加");

                    //用户是否存在
                    SwUser sUser = this.UserAccessor.GetOne(new Filter(SwUser._Name, user.Name, ComparisonOperator.Equal));
                    if (sUser.Name != null)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = "用户已存在";
                        return message;
                    }
                    //不存在则添加
                    try
                    {
                        this.UserAccessor.Insert(user);
                    }
                    catch (Exception ex)
                    {
                        message.MessageType = SwMessageType.Error;
                        message.MessageText = string.Format("添加用户失败，详细信息：{0}",ex.Message);
                    }

                    return message;
                }

                public bool Exist(string userName)
                {
                    if (this.UserAccessor.RecordCount(new Filter(SwUser._Name, userName, ComparisonOperator.Equal)) > 0)
                        return true;
                    else
                        return false;
                }
            }
        }
    }
}